package com.szxy.service.impl;

import com.szxy.mapper.GoodsNoticeMapper;
import com.szxy.mapper.UserFocusMapper;
import com.szxy.mapper.UserMapper;
import com.szxy.pojo.Focus;
import com.szxy.pojo.User;
import com.szxy.service.UserService;
import com.szxy.utils.MD5;
import com.szxy.utils.UserGrid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:zwer
 * @Date:2019/9/2 10:38
 * @Description:com.szxy.service.impl
 * @Version:1.0
 **/
@Service
public class ConcreteUserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserFocusMapper focusMapper;

    @Override
    public int registerCheck(String phone) {
        return this.userMapper.selectByUserPhoneMapper(phone)==null?0:1;
    }

    @Override
    public int userRegisterService(String username, String phone, String password) {
        String md5Password = MD5.md5(password);
        return this.userMapper.inserUserMapper(username,phone,md5Password)>1?1:0;
    }

    @Override
    public User userLoginService(String phone, String password) {
        User user = this.userMapper.selectByUserPhoneMapper(phone);
        if(user != null){
            //密码使用 MD5 加密
            String md5 = MD5.md5(password);
            if(user.getPassword().equals(md5)){
                return user;
            }
        }
        return null;
    }

    @Override
    public void updateUserInfoService(User user) {
        this.userMapper.updateUserInfoMapper(user);
    }

    @Override
    public UserGrid findUserByPaginationService(Integer pageNum, Integer pageSize) {
        Integer start = (pageNum-1)*pageSize;
        Integer end = pageSize;
        List<User> userList = this.userMapper.selUserByPaginationMapper(start, end);
        UserGrid userGrid = new UserGrid();
        userGrid.setCurrent(pageNum);
        userGrid.setRowCount(pageSize);
        userGrid.setRows(userList);
        Integer count = this.userMapper.selCount();
        userGrid.setTotal(count);
        return userGrid;
    }

    @Override
    public User findSellerInfoByIdService(Integer id) {
        return this.userMapper.selSellerInfoByIdMapper(id);
    }

    @Override
    public void addGoodsFocusService(Integer goodId, Integer uid) {
        this.focusMapper.addGoodsFocusMapper(goodId,uid);
    }

    @Override
    public List<Focus> findGoodsFocusByUserIdService(Integer userId) {
        return this.focusMapper.selGoodsFocusByUserIdMapper(userId);
    }

    @Override
    public void deleteFocusGoodsService(Integer goodId) {
        this.focusMapper.delFocusGoodsMapper(goodId);
    }

}
